// https://leetcode.cn/problems/minimum-absolute-difference-in-bst/?envType=study-plan-v2&envId=top-interview-150

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    int ans;
public:
    void inorder(TreeNode* root, int& prev) {
        if (root == nullptr) {
            return;
        }

        inorder(root->left, prev);

        if (prev == -1) {
            prev = root->val;
        } else {
            ans = min(ans, root->val - prev);
            prev = root->val;
        }

        inorder(root->right, prev);
    }

    int getMinimumDifference(TreeNode* root) {
        ans = INT_MAX;
        int prev = -1;
        inorder(root, prev);
        return ans;
    }
};